package com.aaa.member.controller.api;

import com.aaa.entity.Member;
import com.aaa.member.service.RegisterService;
import com.aaa.vo.Result;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author 荆豪峰
 * @version 1.0
 * #Date: 2023/3/6 14:09
 */

@RestController
@RequestMapping("/member/register")
public class RegisterController {
    @Autowired
    private RegisterService registerService;
    private  String codes;
    /**
     * #Description 用户注册
     * @param member
     * @return com.aaa.vo.Result
     * @author 荆豪峰
     * #Date 2023/3/6
     */
    @PostMapping("register")
    public Result insert(@RequestBody Member member){
        if (!member.getCode().equals(codes)) {
            return new Result(5000,"验证码错误");
        }
        return registerService.insert(member);
    }
    /**
     * #Description 发送手机验证码
     * @param phone
     * @return void
     * @author 荆豪峰
     * #Date 2023/3/9
     */
    @PostMapping("/send")
    public void sends(String phone){
        int appid=1400752224 ;
        String appkey = "dcd604f496ca47f62a91e33d822e4c0b";
        int templateId = 1579077;
        String smsSigNum = "海风的Java分享公众号";
        codes = RandomStringUtils.randomNumeric(6);
        SmsSingleSender sender = new SmsSingleSender(appid,appkey);
        try {
            SmsSingleSenderResult result =sender.sendWithParam("86",phone,templateId,
                    new String[]{codes},smsSigNum,"","");
        } catch (HTTPException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * #Description 查询用户手机号
     * @param phoneNum
     * @return com.aaa.vo.Result
     * @author 荆豪峰
     * #Date 2023/3/9
     */
    @PostMapping("/findPhone")
    public Result findPhone(String phoneNum){
        return registerService.findPhone(phoneNum);
    }
    /**
     * #Description 忘记密码修改密码
     * @param member
     * @return com.aaa.vo.Result
     * @author 荆豪峰
     * #Date 2023/3/9
     */
    @PostMapping("/updatepswd")
    public Result updatepswd(@RequestBody Member member) {
        if (!member.getCode().equals(codes)) {
            return new Result(5000,"验证码错误");
        }
        return registerService.updatepswd(member);
    }
}
